

clear all
use "[Folder]\Panel_Firm_Lab_OccFE_temp.dta" 


//Set nbr of parameters
	global MLab = 129
	global FLab = 2
	distinct Ind if u==0
	global Inds = r(ndistinct) - 1
	global Sizes = 4
	global Cities = 42
	global Years = 6
	global Res = 16
	global NbrOfParam =  $MLab + $FLab + $Cities + $Sizes + $Years + $Inds + $Res
	disp $NbrOfParam

//Set natives to reference group
gen RefGr= lab_M_107 
drop lab_M_107 
rename lab_M_1 lab_M_107 


//Function evaluation program
     program nlceslab
                version 14.2
                syntax varlist [if], at(name)
                local log_VA: word 1 of `varlist'
	
	// Retrieve parameters out of at matrix				
				tempvar LabAgg temp_VA
				gen double `LabAgg' = 0 `if'
                gen double `temp_VA' = 0 `if'
				replace `LabAgg' = `LabAgg' + RefGr  `if'

				
				forvalues i=2(1)$MLab {
				tempname delta 
				scalar `delta' = `at'[1,`i']
				replace `LabAgg' = `LabAgg' + `delta' * (lab_M_`i')  `if'
				}
				
				tempname delta 
				scalar `delta' = `at'[1,$MLab +1]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_FNat)  `if'
	
				tempname delta 
				scalar `delta' = `at'[1,$MLab +2]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_FFor)  `if'
	
	
				//Adj index for number of industry dummies
				forvalues i=1(1)$Inds {
				tempname Industry 
				scalar `Industry' = `at'[1,$MLab + $FLab + `i']
				replace `temp_VA' = `temp_VA' + `Industry'*Ind`i' 	`if'			
				}
				//Adj index for number of firm size dummies
				forvalues i=1(1)$Sizes {
				tempname Size 
				scalar `Size' = `at'[1, $MLab + $FLab  + $Inds +`i']
				replace `temp_VA' = `temp_VA' + `Size'*FirmSize_`i' `if'				
				}
				//Adj index for number of City dummies
				forvalues i=1(1)$Cities {
				tempname City 
				scalar `City' = `at'[1, $MLab + $FLab  + $Inds + $Sizes +`i']
				replace `temp_VA' = `temp_VA' + `City'*k_`i' 	`if'			
				}
				//Adj index for number of year dummies
				forvalues i=1(1)$Years {
				tempname Year 
				scalar `Year' = `at'[1, $MLab + $FLab  + $Inds + $Sizes +$Cities +`i']
				replace `temp_VA' = `temp_VA' + `Year'*y_`i' `if'
				}				


                
				// Now fill in dependent variable

                tempname ic thetaL Cap Cap_Sq Cap_Qu TI TI_Sq TI_Qu TICap TICap_Sq TICap_Qu TI_SqCap TI_SqCap_Sq TI_SqCap_Qu TI_QuCap TI_QuCap_Sq TI_QuCap_Qu 
				scalar `ic' = `at'[1,1]
				scalar `thetaL' = `at'[1,$MLab + $FLab + $Inds + $Sizes +$Cities + $Years + 1]
				scalar `Cap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 2]
				scalar `Cap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 3]
				scalar `Cap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 4]
				
				scalar `TI' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 5]
				scalar `TI_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 6]
				scalar `TI_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 7]
			
				scalar `TICap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 8]
				scalar `TICap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 9]
				scalar `TICap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 10]
				scalar `TI_SqCap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 11]
				scalar `TI_SqCap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 12]
				scalar `TI_SqCap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 13]
				scalar `TI_QuCap' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 14]
				scalar `TI_QuCap_Sq' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 15]
				scalar `TI_QuCap_Qu' = `at'[1,$MLab + $FLab +  $Inds + $Sizes +$Cities + $Years + 16]
				
				
				replace `temp_VA' = `temp_VA' + `ic' + `thetaL' *ln(`LabAgg') +  `Cap'*logCap + `Cap_Sq'*logCap_Sq + `Cap_Qu'*logCap_Qu + `TI'*logGrosInvTot + `TI_Sq'*logGrosInvTot_Sq + `TI_Qu'*logGrosInvTot_Qu  + `TICap'*logGrosInvTot*logCap + `TICap_Sq'*logGrosInvTot*logCap_Sq + `TICap_Qu'*logGrosInvTot*logCap_Qu + `TI_SqCap'*logGrosInvTot_Sq*logCap + `TI_SqCap_Sq'*logGrosInvTot_Sq*logCap_Sq +`TI_SqCap_Qu'*logGrosInvTot_Sq*logCap_Qu +`TI_QuCap'*logGrosInvTot_Qu*logCap + `TI_QuCap_Sq'*logGrosInvTot_Qu*logCap_Sq + `TI_QuCap_Qu'*logGrosInvTot_Qu*logCap_Qu  `if'
				replace `log_VA' = `temp_VA'   `if'
            end
			
			
		
cd "[Output Folder]\"
			
mat M = J(1,$NbrOfParam,1)
//set trace on
nl ceslab @ logFtg_Foradlingsvarde if u==0, nparameters($NbrOfParam) initial(M)

parmest, format(estimate min95 max95 %8.2f p %8.1e) saving(HC_OccFE, replace) omit empty 

use "[Output Folder]\HC_OccFE.dta", clear

drop parm
gen par_nr= regexr(eq,"b","")
destring par_nr, replace
replace par_nr = 0 if par_nr ==1
replace par_nr = 1 if par_nr ==107
replace par_nr = 300 if par_nr ==130
replace par_nr = 301 if par_nr ==131
keep if par_nr<130 

rename par_nr FodelseLandnamn

rename estimate HC
drop if stderr==0
keep  HC  FodelseLandnamn

merge 1:1 FodelseLandnamn using "[Folder]\MacroData_ToMona_v1"
drop if _merge==2
drop _merge

merge 1:1 FodelseLandnamn using "[Output Folder]\Weights_Tot"
drop if _merge==2
drop _merge

reg HC TR SS [aw=Weights] if WVInd==1 & cognitive!=. 

reg HC TR SS LifeExp FertRate cognitive [aw=Weights] if WVInd==1 & cognitive!=. 


///////////////////////////////////////////////////////////////
